<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit" />
  <meta name="force-rendering" content="webkit" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />

  <style>
    .form {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-bottom: 1em;
}

label {
  font-weight: bold;
  margin-right: 1em;
}

/* put, select */
input, select {
  padding: 0.5em;
  border: 1px solid #ccc;
  border-radius: 0.25em;
  margin-bottom: 0.5em;
  font-size: 1em;
}

select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath fill='%23333' d='M12.19 6.19L8 10.38l-4.19-4.19L2 7.44l6 6 6-6z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.5em center;
  background-size: 1em auto;
}
.selectNumber{
  width: 4em;
}

.inputNumber{
  /* width: 10em; */
  width: 20em;
}
  </style>

</head>

<body>

  <div class="form">
    <label for="input">输入列表：</label>
    <input class="inputNumber" type="text" id="input" name="input" value="5, 19, 25, 45, 30, 24, 15, 60, 16, 27, 1">
  
    <!-- 备忘录长度 -->
    <label for="memo">内存长度：</label>
    <select id="memo" name="memo" class="selectNumber">
      <option value="5" selected>5</option>
      <option value="10">10</option>
      <option value="15">15</option>
      <option value="20">20</option>
      <option value="25">25</option>
    </select>
  </div>
  
  <button onclick="ReplaceSelectTest()">外排序-置换选择排序解析</button>

  <button onclick="genCode()">genCode</button>
  
  <div id="showDoc"></div>
  <!-- ReplaceSelectTest -->

  <script>
    // import requests
    // # baseUrl='http://localhost:8004/api'
    // baseUrl = 'http://localhost:8003/api'
    baseUrl = 'http://localhost:8889/all'
    // http://localhost:8889/all/genCode
    // {
    // "table_schema": "exam"
    // }
    // {
    // "table_schema": "public"
    // }

    entityName = 'eyesightRes'
    // # path='EyesightResListGetCreateTime'
    path = 'EyesightResListGetCreateTimeEyesightRes'
    // path = 'save'
    let dateList = [
      "2022-02-23 13:13:08",
      "2022-03-23 13:11:55",
      "2022-04-23 13:13:23",
      "2022-05-04 12:57:43",
      "2022-05-19 13:03:10",
      "2022-06-23 13:11:31",
      "2022-06-23 13:12:26",
      "2022-08-23 13:13:39",
      "2022-10-01 13:00:57",
      "2022-12-23 13:12:10",
      "2023-03-02 12:57:14",
      "2023-03-30 13:10:53",
      "2023-05-23 13:04:55",
      "2023-05-23 13:06:37",
      "2023-06-22 13:11:05"
    ]
    // save
    left_eye = 5.0
    right_eye = 5.0
    user_name = "starp"
    // fetch()

    // Example POST method implementation:
    async function postData(url = '', data = {}) {
      // Default options are marked with *
      const response = await fetch(url, {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, *cors, same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, *same-origin, omit
        headers: {
          'Content-Type': 'application/json'
          // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: 'follow', // manual, *follow, error
        referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data) // body data type must match "Content-Type" header
      });
      return response.json(); // parses JSON response into native JavaScript objects
    }

    function addDateZero(num) {
      return (num < 10 ? '0' + num : num)
    }
    // 日期必须是 符合格式的 但是金数据填表的就不是正常数据啊
    function formatDateTime(date) {
      if (!!date) {
        const d = new Date(date);
        return d.getFullYear() + '-' + addDateZero(d.getMonth() + 1) + '-' + addDateZero(d.getDate()) + ' ' +
          addDateZero(d.getHours()) + ':' + addDateZero(d.getMinutes()) + ':' + addDateZero(d.getSeconds());
      } else {
        return '';
      }
    }

    // postData('https://example.com/answer', { answer: 42 })
    //   .then(data => {
    //     console.log(data); // JSON data parsed by `data.json()` call
    //   });

    // function changeDate(records) {
    //   // DateUtil
    //   // TimeUtil
    //   // DateUtil
    //   let res=[]
    //   for (let o of records) {
    //     // DateUtil.
    //     // formatDateTime
    //     res.push(
    //       formatDateTime(o)
    //     )
    //     // o.createTimeStr = formatDateTime(o);

    //     // o.createTimeStr = formatDateTime(o.createTime);
    //     // o.createTime = this.$moment(o.createTime).format('YYYY-MM-DD HH:mm:ss')
    //   }
    //   return res
    // },

    function doSave(dataPost) {
      // let  dataPost= {
      //    "createTime":null ,
      //    "createUserId":null ,
      //    "departId":null ,
      //    "departName":null ,
      //    "deviceId":null ,
      //    "deviceName":null ,
      //    "enableMark":null ,
      //    "id":null ,
      //    "isDeleted":null ,
      //    "leftEyesight":null ,
      //    "memo":null ,
      //    "rightEyesight":null ,
      //    "testTime":null ,
      //    "updateTime":null ,
      //    "updateUserId":null ,
      //    "userCode":null ,
      //    "userId":null ,
      //    "userName":null ,
      //    "userSex":null
      //             }


      postData(`${baseUrl}/${entityName}/save`, dataPost)
        .then(data => {
          console.log("save done");
          console.log(data); // JSON data parsed by `data.json()` call
          // formatDateTime

          // data = data.response
          // data.sort((a, b) => a - b)
          // let dates = data.map(o => {
          //   return formatDateTime(o);
          // })

          // console.log(dates)
        });
    }

    function saveDatesEye(dateList) {
      for (let date of dateList) {
        let dataPost = {
          "createTime": date,
          "createUserId": null,
          "departId": null,
          "departName": null,
          "deviceId": null,
          "deviceName": null,
          "enableMark": null,
          "id": null,
          "isDeleted": null,
          "leftEyesight": left_eye,
          "memo": null,
          "rightEyesight": right_eye,
          "testTime": null,
          "updateTime": null,
          "updateUserId": null,
          "userCode": 1,
          "userId": null,
          "userName": user_name,
          "userSex": null
        }

        doSave(dataPost)
      }

    }
// {
//     "table_schema": "exam"
//     }


// main --======================
    
let djangoUrl='http://43.142.150.223:8080'
 const genCode= ()=>{
  // "table_schema": "exam"
    postData(`http://localhost:8889/all/genCode`,{
    "table_schema": "hotel"
    })
      .then(res => {
        console.log(res); // JSON data parsed by `data.json()` call
        // formatDateTime

        // data = data.response
        // data.sort((a, b) => a - b)
        // // saveDatesEye(data)
        // let datesToSave = data.map(o => {
        //   return new Date(o);
        // })
        // console.log('datesToSave')
        // console.log(datesToSave)
        // saveDatesEye(datesToSave)
        // let dates = data.map(o => {
        //   return formatDateTime(o);
        // })
        // console.log('dates')

        // console.log(dates)
      });

    }
    const ReplaceSelectTest= ()=>{
  // "table_schema": "exam"
  // let  input_lst=[1,3,13,1,31,3,1]
  // let  input_lst=[5, 19, 25, 45, 30, 24, 15, 60, 16, 27, 1]
  // let memo_len=5

  const inputElem = document.getElementById('input');
  const memoElem = document.getElementById('memo');

  const input_lst = inputElem.value.split(',').map(str => parseInt(str.trim()));
  const memo_len = parseInt(memoElem.value);
  // let memo_len=input_lst.length
    postData(`${djangoUrl}/ReplaceSelectTest`,{
      input_lst,
      memo_len,
    "table_schema": "hotel"
    })
      .then(res => {
        console.log(res); 
        let  dataList=
        res.data 
        let showStr=""
        for(let o of dataList){
          // showStr+=o+"\n"
          // showStr+=o+""
          showStr+=`<div>${o}</div>\n`
        }
        // showDoc
        let  showDocDom=document.getElementById("showDoc")
        showDocDom.innerHTML=showStr
        // document.getElementById("showDoc").value=showStr
      });

    }


    // postData(`${baseUrl}/${entityName}/${path}`, {
    //     answer: 42
    //   })
    //   .then(data => {
    //     console.log(data); // JSON data parsed by `data.json()` call
    //     // formatDateTime

    //     data = data.response
    //     data.sort((a, b) => a - b)
    //     // saveDatesEye(data)
    //     let datesToSave = data.map(o => {
    //       return new Date(o);
    //     })
    //     console.log('datesToSave')
    //     console.log(datesToSave)
    //     saveDatesEye(datesToSave)
    //     let dates = data.map(o => {
    //       return formatDateTime(o);
    //     })
    //     console.log('dates')

    //     console.log(dates)
    //   });

    // let data = await postData(`${baseUrl}/${entityName}/${path}`, {
    //   answer: 42
    // })
    // console.log(data);
  </script>

</body>

</html>